﻿import mx.utils.Delegate;
dynamic class FaceBox extends MovieClip {
	
	public var facepic:pictureBox;
	public var mainBj:MovieClip;
	public var faceMaskMc:MovieClip;
	public var mouse:MovieClip;
	public var points:Array;

	private var mouseObje:Object;

	private var __faceurl:String = "";
	private var __scale:Number = 100;
	private var __rotation:Number = 0;
	
	
	private var __worktype:Boolean = true;
	
	public function get WorkType():Boolean {
		return this.__worktype;
	}
	public function set WorkType(value:Boolean) {
		this.__worktype = value;
	}
	public function get FaceUrl():String {
		return this.__faceurl;
	}
	public function set FaceUrl(value:String) {
		this.__faceurl = value;
		this.facepic.PicUrl = value;
		if (value == undefined || value == "") {
			this.facepic.unload();
		}
		this["facep"]._x = this.mainBj._width / 2 + this.mainBj._x;
		this["facep"]._y = this.mainBj._height / 2 + this.mainBj._y;
		this.facepic.load();
	}
	public function get Scale():Number {
		return this.__scale;
	}
	public function set Scale(value:Number) {
		this.__scale = value;
		this["facep"]._xscale = this["facep"]._yscale = value;
	}
	
	public function get Rotation():Number {
		return this.__rotation;
	}
	public function set Rotation(value:Number) {
		this.__rotation = value;
		this["facep"]._rotation = value;
	}
	
	public function FaceBox() {
		initself();
	}
	private function initself() {
		mouseObje = new Object();
		mouseObje.onMouseMove = Delegate.create(this, this.mousemovefun);
		Mouse.addListener(mouseObje);
		
		mainBj = this.bj;
		mouse = this.mouseUI;
		mouse.swapDepths(2000);
		
		/**/
		this.points = new Array();
		for (var i = 0; i < 8; i++) {
			this.points.push(this["point" + i]);
			this["point" + i].onRelease = this["point" + i].onReleaseOutside = this.pointRelease;
			this["point" + i].onPress = this.pointPress;
			this["point" + i]["root"] = this;
			this["point" + i].swapDepths(i + 100);
		}
		
		trace(mainBj)
		
		this.createEmptyMovieClip("facep", 10);
		this.facepic = new pictureBox(this["facep"]);
		/**/
		this.facepic.Width = this.mainBj._width;
		this.facepic.Height = this.mainBj._height;
		//this.facepic.Scal = "both";
		this.facepic.Scal = "height";
		
		this["facep"]._x = this.mainBj._width / 2 + this.mainBj._x;
		this["facep"]._y = this.mainBj._height / 2 + this.mainBj._y;
		
		//this["facep"].onRelease = this.pointRelease;
		//this["facep"].onPress = this.pointPress;
		
		this[pictureBox.M_PROGRESS] = Delegate.create(this, this.picloadProcess);
		this.facepic.addListener(this);
		
		faceMaskMc = this.createEmptyMovieClip("faceMask", 3000);
		
		drawFaceMask();
	}
	private function picloadProcess(ll,tt) {
		if (ll == tt && ll > 0) {
			var picm:MovieClip = this["facep"]["___pic"];
			picm._x = -picm._width / 2;
			picm._y = -picm._height / 2;
			picm.onRelease = this.pointRelease;
			picm.onPress = this.pointPress;
			picm.root = this;
		}
	}
	
	private function pointPress() {
		//this.startDrag(false, -2,-2,155,165)
		if (this.root.__worktype) {
			this._drag = true;
			this.startDrag();
		}
	}
	private function pointRelease() {
		this._drag = false;
		this.stopDrag();
		this._parent.drawFaceMask();
	}
	private function drawFaceMask() {
		faceMaskMc.clear();
		faceMaskMc.beginFill(0, 50);
		faceMaskMc.moveTo(this.points[0]._x, this.points[0]._y);
		for (var i = 1; i < 8; i++) {
			var i1 = i;
			var i2 = (i + 1)%8;
			faceMaskMc.curveTo(this.points[i1]._x, this.points[i1]._y, this.points[i2]._x, this.points[i2]._y);
			i++;
		}
		faceMaskMc.endFill();
		this["facep"].setMask(faceMaskMc);
	}
	
	
	private function mousemovefun() {
		if(this.__worktype){
			if (this.mainBj.hitTest(_root._xmouse, _root._ymouse, true)) {
				Mouse.hide();
				mouse._x = this._xmouse;
				mouse._y = this._ymouse;
			}else {
				if (this["facep"]["___pic"]._drag == true) {
					this["facep"]["___pic"]._drag = false;
					this["facep"]["___pic"].stopDrag();
				}
				
				/**/
				for (var i = 0; i < this.points.length; i++) {
					//this.points[i].stopDrag();
					if (this.points[i]._drag == true) {
						this.points[i]._drag = false;
						this.points[i].stopDrag();
					}
				}
				Mouse.show();
			}
		}else {
			mouse._visible = false;
			Mouse.show();
		}
	}
	
	
	public function get FaceData():Object {
		return getFaceData();
	}
	public function set FaceData(Obj:Object) {
		this.setFaceData(Obj);
	}
	private function getFaceData():Object {
		var obj:Object = new Object();
		var pointlk:Array = new Array();
		for (var i = 0; i < this.points.length; i++) {
			pointlk.push(this.points[i]._x);
			pointlk.push(this.points[i]._y);
		}
		obj.points = pointlk.join("|");
		obj.faceurl = this.FaceUrl;
		obj.facescale = this.Scale;
		obj.facerotation = this.Rotation;
		obj.faceXY = this["facep"]["___pic"]._x + "|" + this["facep"]["___pic"]._y;
		return obj;
	}
	
	private function setFaceData(faceObj:Object) {
		//trace("--------------"+faceObj.faceurl)
		if (faceObj.faceurl == undefined || faceObj.faceurl == "") {
			this.facepic.unload();
			return;
		}
		var pointsl:String = faceObj.points;
		var faceurl:String = faceObj.faceurl;
		var facescale:String = faceObj.facescale;
		var facerotation:String = faceObj.facerotation;
		var faceXY:String = faceObj.faceXY;
		if (pointsl == undefined) {
			pointsl = "";
		}
		if (facescale == undefined || facescale == "") {
			facescale = "100";
		}
		if (facerotation == undefined || facerotation == "") {
			facerotation = "0";
		}
		if (faceXY == undefined || faceXY == "") {
			faceXY = (this.mainBj._width / 2 + this.mainBj._x).toString()+"|"+(this.mainBj._height / 2 + this.mainBj._y);
			//faceXY = "0|0";
		}
		
		this.FaceUrl = faceurl;
		
		
		var pointlks:Array = pointsl.split("|");
		for (var i = 0; i < pointlks.length; i++) {
			this.points[int(i/2)]._x = Number(pointlks[i]);
			this.points[int(i/2)]._y = Number(pointlks[++i]);
		}
		
		this.Scale = Number(facescale);
		this.Rotation = Number(facerotation);
		var xy:Array = faceXY.split("|");
		this["facep"]["___pic"]._x = Number(xy[0]);
		this["facep"]["___pic"]._y = Number(xy[1]);
		
		this.drawFaceMask();
	}
	
	
	
	
	
	
	
	
	
	
	
	
}